<?php


namespace app\home\controller;

use app\home\controller\Base;
use app\home\model\User as muser;
use think\Db;
use think\facade\Config;
use app\common\library\Hash;
use app\common\library\Yunpian;
use think\Validate;

class Homeuser extends Base
{
    protected $beforeActionList = [
        'first',
    ];

    protected function first()
    {
        if (session('uid')) {
            $this->assign('nav', 'user');
        } else {
            $this->redirect('/home/user/index');
        }

    }

    public function userinfo()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = Validate::make([
                'mobile|手机号码' => 'require|mobile',
                'repassword|重复密码' => 'confirm:password',
                'password|密码' => 'length:6,16',
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if (session('mobile_code_time') < time()) {
                $this->error('短信验证码已经超时或没有发送成功');
            }
            if (session('mobile_code') != 'm' . $post['mobile'] . $post['code']) {
                $this->error('短信验证码错误');
            }
            $info = DB::name('user')->where('id', $post['id'])->find();
            if ($info['mobile'] == $post['mobile']) {
                unset($post['mobile']);
            } else {
                $old_mobile = DB::name('user')->where('mobile', $post['mobile'])->find();
                if ($old_mobile) {
                    $this->error('和已经注册的用户手机号码相同，如果是你注册请找回密码或更换手机号码事情');
                }
            }
            $user = new muser();
            if (isset($post['password']) && !empty($post['password'])) {
                $post['password'] = Hash::hash($post['password']);
            } else {
                unset($post['password']);
            }
            if ($user->where('id', $post['id'])->strict(false)->update($post) !== false) {
                session('mobile_code_time', null);
                session('mobile_code', null);
                $this->success('恭喜您资料修改成功');
            }
            $this->error('资料修改失败，请稍后再试');

        } else {
            $info = DB::name('user')->where('id', session('uid'))->find();
            $this->assign('info', $info);
            return view();
        }


    }

    public function checking()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = Validate::make([
                'company|公司名称' => 'require',
                'tax|统一社会信用代码' => 'require'
            ]);
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            if (DB::name('user')->strict(false)->where('id', session('uid'))->update($post)) {
                $this->success('操作成功!');
            }
            $this->error('操作失败或没有发生变化');

        } else {
            $info = DB::name('user')->where('id', session('uid'))->find();
            $this->assign('info', $info);
            return view();
        }


    }

    public function index()
    {
        if (session('uid')) {
            $user = new muser();
            $info = $user->where('id', session('uid'))->find();
            $info['last_login_time'] = date('Y-m-d  H:i:s', $info['last_login_time']);
            $this->assign('info', $info);
            return view();
        } else {
            $this->redirect('/home/user/index');
        }
    }

    public function logout()
    {
        session('username', null);
        session('uid', null);
        $this->redirect('/home/user/index');

    }


}